<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 用户输入内容 -->
        <input type="text" v-model="info" @keyup.enter="addInfo">

        <!-- 未完成 -->
        <com :props_list="list" @sondelevent="fathercallback" @soncheckevent="fathercheckcallback">
            <template #title="slotScope">
                <h1>{{slotScope.$title.title1}}</h1>
            </template>
        </com>
        <!-- 已完成 -->
        <com :props_list="doneList" @sondelevent="fathercallback" @soncheckevent="fathercheckcallback">
            <template #title="slotScope">
                <h1>{{slotScope.$title.title2}}</h1>
            </template>
        </com>
    </div>
    <script src="./js/vue.js"></script>
    <script>
        let com = {
            data() {
                return {
                    titleinfo: {
                        title1: "未完成",
                        title2: "已完成"
                    }
                }
            },
            props: ["props_list"],
            template: `
            <div class="todoListBox">
                <slot name="title" :$title="titleinfo"></slot>
                <div v-for="(item, index) in props_list" :key="item.id">
                    <input type="checkbox" v-model="item.isChecked" @change="checkstatus(item)">
                    <span>{{item.contents}}</span>
                    <button @click="delFn(item)">删除</button>
                </div>
            </div>
            `,
            methods: {
                checkstatus(item){
                    this.$emit("soncheckevent",item)
                },
                delFn(item){
                    // this.props_list.splice(this.props_list.indexOf(item),1)
                    this.$emit("sondelevent",item)
                }
            }
        }
        let vm = new Vue({
            el: "#app",
            data: {
                info: "",
                id: 1,
                // 未完成
                list: [],
                // 已经完成
                doneList: []
            },
            methods: {
                addInfo() {
                    // 插入数据
                    this.list.push({
                        id: this.id++,
                        contents: this.info,
                        isChecked: false
                    });
                    // 清空数据
                    this.info = "";
                },

                fathercallback(res){

                    if(res.isChecked){
                        this.doneList.splice(this.list.indexOf(res),1)
                    }else{
                        this.list.splice(this.list.indexOf(res),1)
                    }
                },
                fathercheckcallback(res){
                    if(res.isChecked){
                        this.doneList.push(res)
                        this.list.splice(this.list.indexOf(res),1)
                    }else{
                        this.list.push(res)
                        this.doneList.splice(this.doneList.indexOf(res),1)
                    }
                }
            },
            components: {
                com
            }

        })
    </script>
</body>
</html>